/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2020-06-26
 * Time: 19:33
 */
#include <iostream>
#include <boost/progress.hpp>
#include <boost/static_assert.hpp>

using namespace boost;
using namespace std;

template<int N = 2>
class new_progress_timer : public boost::timer{
public:
    new_progress_timer(std::ostream &os = std::cout) : m_os(os) {
        BOOST_STATIC_ASSERT(N >= 0 && N <= 10);
    }

    virtual ~new_progress_timer() {
        try {
            //保存流的状态
            std::istream::fmtflags old_flags = m_os.setf(std::istream::fixed, std::istream::floatfield);
            std::streamsize old_prec = m_os.precision(N);

            //输出时间
            m_os << elapsed() << "s\n" << std::endl;

            //恢复流状态
            m_os.flags(old_flags);
            m_os.precision(old_prec);
        }
        //析构函数绝对不能破除异常
        catch (...) {}
    }
private:
    std::ostream &m_os;
};

//模板特例化
template<>
class new_progress_timer<2> : public boost::progress_timer{};

int main() {
    new_progress_timer<> t;
    //new_progress_timer<10> t1;


    return 0;
}